<template>

    <div>
        <p>
            <button v-show="hasResource('010101')" @click="list" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-refresh green"></i>
                刷新
            </button>
            &nbsp;
            <button v-show="hasResource('010102')" @click="add" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-edit red"></i>
                新增
            </button>
        </p>
        <pagination ref="pagination" v-bind:list="list"></pagination>
        <table id="simple-table" class="table  table-bordered table-hover">
            <thead>
            <tr>
                        <th>id</th>

                        <th>登录名</th>

                        <th>昵称</th>

                        <th>密码</th>
                <th>操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="user in users">
                            <td>{{user.id}}</td>
                            <td>{{user.loginName}}</td>
                            <td>{{user.name}}</td>
                            <td>{{user.password}}</td>

                <td>
                    <div class="hidden-sm hidden-xs btn-group">
                        <button v-show="hasResource('010104')" @click="changePwd(user)" class="btn btn-white btn-sm btn-danger">
                            <i class="ace-icon fa fa-undo bigger-120"></i>修改密码
                        </button>

                        <button v-show="hasResource('010102')" @click="edit(user)" class="btn btn-white btn-sm btn-info">
                            <i class="ace-icon fa fa-pencil bigger-120"></i>编辑
                        </button>

                        <button v-show="hasResource('010103')" @click="handledelete(user.id)" class="btn btn-white btn-sm btn-danger">
                            <i class="ace-icon fa fa-trash-o bigger-120"></i>删除
                        </button>

                    </div>

                    <div class="hidden-md hidden-lg">
                        <div class="inline pos-rel">
                            <button @click="changePwd(user)" class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown"
                                    data-position="auto">
                                <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                            </button>

                            <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                <li v-show="hasResource('010104')">
                                    <a href="#" class="tooltip-info" data-rel="tooltip" title="修改密码">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                                    </a>
                                </li>

                                <li v-show="hasResource('010102')">
                                    <a @click="edit(user)" class="tooltip-success" data-rel="tooltip" title="编辑">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                                    </a>
                                </li>

                                <li v-show="hasResource('010103')">
                                    <a @click="handledelete(user.id)" class="tooltip-error" data-rel="tooltip" title="删除">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
        <!-- /.span -->

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">登录名:</label>
                                                <div class="col-sm-10">
                                                    <input type="text" v-model="userinfo.loginName" v-bind:disabled="userinfo.id" class="form-control" placeholder="登录名">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">昵称:</label>
                                                <div class="col-sm-10">
                                                    <input type="text" v-model="userinfo.name" class="form-control" placeholder="昵称">
                                                </div>
                                            </div>
                                            <div class="form-group" v-show="!userinfo.id">
                                                <label class="col-sm-2 control-label">密码:</label>
                                                <div class="col-sm-10">
                                                    <input type="password" v-model="userinfo.password" class="form-control" placeholder="密码">
                                                </div>
                                            </div>

                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" @click="save" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

        <div class="modal fade" id="changepwd-modal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">修改密码</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">密码:</label>
                                                <div class="col-sm-10">
                                                    <input type="password" v-model="userinfo.password" class="form-control" placeholder="密码">
                                                </div>
                                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" @click="savePwd" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->


    </div><!-- /.row -->


</template>


<script scope>
    import axios from 'axios';
    import {defineComponent, onMounted, ref} from 'vue';
    import Pagination from '@/components/pagination.vue';

    export default defineComponent({
        name: 'user',
        components:{
            Pagination
        },
        setup() {
            const users = ref();
            const pagination = ref({});
            const userinfo=ref({});
            const list = () => {
                Loading.show();
                axios.get("/system/admin/user/list", {
                    params: {
                        page: pagination.value.page,
                        size: pagination.value.size,
                    }
                }).then((response) => {
                    Loading.hide();
                    const data=response.data;
                    if (data.success){
                        users.value = data.content.list;
                        pagination.value.render(pagination.value.page,data.content.total);
                    }else {
                        Toast.error(data.message);
                    }

                })
            };

            const add=()=>{
                userinfo.value={};
                $('#myModal').modal();
            };

            /**
             * 编辑
             */
            const edit = (user) => {
                $('#myModal').modal();
                userinfo.value = Tool.copy(user);
            };

            /**
             * 修改密码
             */
            const changePwd = (user) => {
                userinfo.value.password="";
                userinfo.value.id=user.id;
                $('#changepwd-modal').modal();
            };
            /**
             * 删除
             */
            const handledelete = (id) => {
                Confirm.show("删除用户后不可恢复，确认删除？", function () {
                    Loading.show();
                    axios.delete("/system/admin/user/delete/" + id).then((response) => {
                        Loading.hide();
                        const data = response.data;
                        if (data.success) {
                            list();
                            Toast.success("删除成功!");
                        }else{
                            Loading.hide();
                            Toast.warning(data.message);
                        }
                    })
                });
            };

            const save=()=>{
                // 保存校验
                if (1 != 1
                    || !Validator.require(userinfo.value.loginName, "登录名")
                    || !Validator.length(userinfo.value.loginName, "登录名", 1, 50)
                    || !Validator.length(userinfo.value.name, "昵称", 1, 50)
                    || !Validator.require(userinfo.value.password, "密码")
                    || !Validator.length(userinfo.value.password, "密码", 1, 32)
                ) {
                    return;
                }
                //密码加密
                userinfo.value.password=hex_md5(userinfo.value.password+KEY);
                Loading.show();
                axios.post("/system/admin/user/save",userinfo.value).then((response) => {
                    const data=response.data;
                    if (data.success){
                        Loading.hide();
                        $('#myModal').modal('hide');
                        // console.log(data);
                        list();
                        Toast.success("保存成功！");
                    }else{
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };

            //保存密码
            const savePwd=()=>{
                // 保存校验
                if (1 != 1
                    || !Validator.require(userinfo.value.password, "密码")
                    || !Validator.length(userinfo.value.password, "密码", 1, 32)
                ) {
                    return;
                }
                //密码加密
                userinfo.value.password=hex_md5(userinfo.value.password+KEY);
                Loading.show();
                axios.post("/system/admin/user/save-password",userinfo.value).then((response) => {
                    const data=response.data;
                    if (data.success){
                        Loading.hide();
                        $('#changepwd-modal').modal('hide');
                        list();
                        Toast.success("保存成功！");
                    }else{
                        Loading.hide();
                        Toast.warning(data.message);
                    }

                })
            };

            //js中的hasResource
            const hasResource=(id)=>{
                return Tool.hasResource(id);
            }


            onMounted(() => {
                list();
            });

            return {
                list,
                add,
                edit,
                handledelete,
                pagination,
                userinfo,
                save,
                users,
                changePwd,
                savePwd,
            hasResource
            }


        }
    });
</script>